package be.djdb.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Projectresources{
	/**
	* @author Lieven Roegiers
	* @copyright 2011
	* @from JAVA_mylibs
	* @from http://code.google.com/p/javamylibs/
	*/
	private static ResourceBundle resourceslbl;
	private static ResourceBundle resourcestooltip;
	private static ResourceBundle resourcesERROR;
	private static final boolean DEBUG = false;
	private static final boolean FOLOW = false;
	private boolean lbl=true,tooltip=true,error=true;
	private OutputStreamWriter outstr;
	public Projectresources(String resourcebundlename){
		try{
			resourceslbl = ResourceBundle.getBundle((resourcebundlename+"-lbl"));
			resourcestooltip = ResourceBundle.getBundle((resourcebundlename+"-tooltip"));
			resourcesERROR = ResourceBundle.getBundle((resourcebundlename+"-ERROR"));
		}catch (MissingResourceException e){	
			log(Level.ALL, "");
		}
	}
	public Projectresources(String resourcebundlename,OutputStream keysnotfound){
		try{
			resourceslbl = ResourceBundle.getBundle((resourcebundlename+"-lbl"));
			resourcestooltip = ResourceBundle.getBundle((resourcebundlename+"-tooltip"));
			resourcesERROR = ResourceBundle.getBundle((resourcebundlename+"-ERROR"));
			this.outstr = new OutputStreamWriter(keysnotfound);
		}catch (MissingResourceException e){	
			log(Level.ALL, "");
		}
	}
	public String getlblFor(String bundleKey){
	    try {return resourceslbl.getString(bundleKey);
	    } catch (Exception ex) {
	        	if (DEBUG){log(Level.WARNING, "LOCAL_GUIsetup" );}
	        	propertiefilegenerator(bundleKey);
	        	if (FOLOW || lbl){log(Level.SEVERE,"-lbl.properties is not loaded");lbl = false;}
	    }
	    return "? "+ bundleKey+" ?";
	}
	 /**<%@ page contentType="text/html; charset=" %>
	 * @param Bundlename
	 * @return Bundletext;
	 */
	public String getTooltipFor(String bundleKey){
	    try {	    		
            return resourcestooltip.getString(bundleKey);
        } catch (Exception ex) {
        	propertiefilegenerator(bundleKey);
        	if (FOLOW || tooltip){log(Level.SEVERE,"-tooltip.properties is not loaded");tooltip = false;}
        }
	    return null;
	}
	public boolean propertiefilegenerator(String bundleKey){
		if (outstr!=null){
			try {
				outstr.append(bundleKey+" ERROR KEY NOT FOUND \r\n");////////
				return true;
			} catch (IOException e) {
				return false;
			}
		}
		return false;
	}
	public void notfounedkeys(OutputStream outs){
		this.outstr = new OutputStreamWriter(outs);
	}
	public String getERRORtextFor(String bundleKey){
	    try{	    		
            return resourcestooltip.getString(bundleKey);
        }catch(Exception ex){
            log(Level.SEVERE,"-error.properties is not loaded");
            propertiefilegenerator(bundleKey);
        }
        return "? no ERRORMSG for:"+ bundleKey+" ?";
	}
    private static  void log(Level level, String msg ){
        String tag ="<>>>>*JAVA_LIBS-�L Roegiers 80072631156*<<<<>!!!BY JAR!!!/lib<>";
        //this.msg += tag + msg + "<br />";
        Logger.getLogger(Projectresources.class.getName()).log(level, tag + msg);
    }
}
